package com.dongdongsongcan.app.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.dongdongsongcan.app.db.ShoppingCarDao;
import com.dongdongsongcan.app.db.bean.ShoppingCarDBBean;
import com.dongdongsongcan.app.view.BadgeView;

public class ShoppingCarControl {
    private static ShoppingCarControl sShoppingCarControl;

    private ShoppingCarControl() {
    }

    public static ShoppingCarControl i() {
        if (sShoppingCarControl == null) {
            sShoppingCarControl = new ShoppingCarControl();
        }
        return sShoppingCarControl;
    }

    public void save(ShoppingCarDBBean shoppingCarDBBean) {
        ShoppingCarDao.save(shoppingCarDBBean);
    }

    public void delete(int productId) {
        ShoppingCarDao.delete(productId);
    }

    public List<ShoppingCarDBBean> getShoppingCarList(int shopId) {
        return ShoppingCarDao.getDataByShopId(shopId);
    }

    /**
     * 购物车上的描述文字
     *
     * @param shopId
     * @return
     */
    public void setShoppingCarToolbar(int shopId, TextView des, TextView btn_des, BadgeView badgeView) {
        StringBuilder stringBuilder = new StringBuilder();

        List<ShoppingCarDBBean> shoppingCarDBBeans = ShoppingCarDao
                .getDataByShopId(shopId);
        if (shoppingCarDBBeans == null || shoppingCarDBBeans.isEmpty()) {
            badgeView.setVisibility(View.INVISIBLE);
            des.setText("购物车空空如也");
            btn_des.setText("还差69元");
            btn_des.setEnabled(false);
            btn_des.setBackgroundColor(0xFF99E24B);
            return;
        }
        badgeView.setVisibility(View.VISIBLE);
        int totalPrice = 0;
        int count = 0;
        for (int i = 0; i < shoppingCarDBBeans.size(); i++) {
            ShoppingCarDBBean shoppingCarDBBean = shoppingCarDBBeans.get(i);
            totalPrice += shoppingCarDBBean.getPrice()
                    * shoppingCarDBBean.getCount();
            count += shoppingCarDBBean.getCount();
        }
        des.setText(totalPrice + "元");

        int minPrice = 69 - totalPrice;
        if (minPrice > 0) {
            btn_des.setText("还差" + minPrice + "元");
            btn_des.setEnabled(false);
            btn_des.setBackgroundColor(0xFF99E24B);

        } else {
            btn_des.setText("选好了");
            btn_des.setEnabled(true);
            btn_des.setBackgroundColor(0xFFFE2D4B);

        }
        if (count > 0) {
            badgeView.setBadgeCount(count);
        }
    }
}
